<?php
require_once("../lib/Rest.inc.php");
/*
*	http://localhost/RESTphp/Restaurant.php?rquest=selectRestaurant&Id=1
*
*	HTTP://localhost/RESTphp/Restaurant/selectRestaurant/1/json
*	HTTP://localhost/RESTphp/Monan/selectMonan/1/json
*	*class - controll
*	*method - Phuong thuc
*	*params - Tham so
*	*format - Kieu response
*	*author - dang nhap
*	*Quy dinh: GET
*	* cho cac phuong thuc viet select 1 doi tuong la select + class_name
*	* cho cac phuong thuc select all phai la selectAll
*	* cho cac phuong thuc tim kiem la finds
*	* POST or PUT
*	* cho cac phuong thuc them 1 doi tuong la insert + class_name
*	* cho cac phuong thuc them nhieu doi tuong la inserts
*	* PUT
*	* cho cac phuong thuc de cap nhat 1 doi tuong la update + class
*	* cho cac phuong thuc de cap nhat nhieu doi tuong la updates
*	* DELETE
*	* cho cac phuogn thuc xoa 1 doi tuong la delete + class 
*	* cho cac phuong thuc xoa nhieu doi tuong la deleteAll
*/
class AppInterface extends REST
{
	//Class need create
	private $APIclass;
	
	private $class_name;
	
	public function __construct()
	{
		parent::__construct();
		
		//$this->_detected_login();
		
		$this->create_APIclass();
		
		$this->get_Format();
		
		//$this->getAuthor();
		
	}
	public function processAPI($url ='')
	{
		/*switch ($this->_get_request_method())
		{
			case "GET":
				$func = "select";
				if((int)method_exists($this,$func) > 0)
					$this->$func();
				else{
					$error = array('Status'=>'Falied', 'Message'=>'Not Found in Service');
					$this->response($error,404);
					}
				break;
			case "POST":
				$func = "create";
				if((int)method_exists($this,$func) > 0)
					$this->$func();
				else{
					$error = array('Status'=>'Falied', 'Message'=>'Not Found in Service');
					$this->response($error,404);
					}
				break;
			case "PUT":
				$func = "update";
				if((int)method_exists($this,$func) > 0)
					$this->$func();
				else{
					$error = array('Status'=>'Falied', 'Message'=>'Not Found in Service');
					$this->response($error,404);
					}
				break;
			case "DELETE";
				$func = "delete";
				if((int)method_exists($this,$func) > 0)
					$this->$func();
				else{
					$error = array('Status'=>'Falied', 'Message'=>'Not Found in Service');
					$this->response($error,404);
					}
				break;
			default:
				break;
		}*/
		
		$func = isset($_REQUEST['rquest'])?strtolower(trim(str_replace("/","",$_REQUEST['rquest']))):"";
		if((int)method_exists($this,$func) > 0)
			$this->$func();
		else{
			$error = array('Status'=>'Falied', 'Message'=>'Not Found in Service');
			$this->response($error,404);
		}
	}
	public function get_Format()
	{
		$format = isset($_REQUEST['format'])?$_REQUEST['format']:"json";
		$this->set_format($format);
	}
	public function create_APIclass()
	{
		$this->class_name = isset($_REQUEST['api'])?$_REQUEST['api']:"";
		if(empty($this->class_name))
		{
			$error = array('Status'=>'Failed', 'Message'=>'Cant find Object from server' );
			$this->response($error, 500);
		}
		$this->APIclass = &load_class($this->class_name.'.cls','controllers');
	}
	
	/*********************************/
	/*
	*
	*/// GET
	
	private function select() // truy xuat 1 doi tuong cua APIclass
	{
		$this->_check_request_method('GET');
		$id = $this->_request['id'];
		if(!empty($id))
		{
			$func_method = 'select'.$this->class_name;
			$result = $this->APIclass->$func_method($id);
			if(!empty($result))
				$this->response($result,200);
			else
			{
				$error = array ('Status'=>'No Object Found', 'Message'=>'Cant find an Object from Server');
				$this->response($error, 500);
			}
		}else
		{
			$error = array('status' => "Failed", 'Message' => "No Content");
			$this->response($error,204);	// If no records "No Content" status
		}
	}
	
	private function selectAll()
	{
		$this->_check_request_method('GET');
		$func_method = "getAll".$this->class_name;
		$id = isset($this->_request['id'])?$this->_request['id']:"";
		$result = $this->APIclass->$func_method($id);
		if(!empty($result))
		{			
			$this->response($result,200);
		}
		else
		{
			$error = array('status' => "Failed", 'Message' => "No Content");
			$this->response($error,204);	// If no records "No Content" status
		}	
	}
	
	/*********************************/
	/*
	*
	*/ // PUT or POST
	
	private function insert()
	{
		if($this->_check_request_method('POST')){
			$data = $this->parseIncomingParams();
			$success = array('Status'=>'Success', 'Data'=> $data, 'Message'=>'Complete to insert this');
			$this->response($data, 200);
			$func_method = 'insert'.$this->class_name;
			if ($this->APIclass->$func_method($data))
			{
				$success = array('Status'=>'Success', 'Data'=> $data, 'Message'=>'Complete to insert this');
				$this->response($success, 200);
			}else
			{
				$error = array('Status' => "Failed", 'Message' => "Cant insert into service because missing information");
				$this->response($error,204);
			}
		}
	}
	
	private function inserts()
	{
		
	}
	
	/*********************************/
	/*
	*
	*/ // POST or PUT
	
	private function update()
	{
		if($this->_check_request_method('POST') || $this->_check_request_method('PUT')){
			$id = $_REQUEST['id'];
		
			$func_method = 'update'.$this->class_name;
			if(!empty($id)){
				$data = $this->parseIncomingParams();
				$data['id'] = $id; 
				if($this->APIclass->$func_method($data))
				{
					$success = array('Status'=>'Success', 'Message'=>'Complete to update this object');
					$this->response($success, 200);
				}else
				{
					$error = array('Params'=>$data,'status' => "Failed", 'Message' => "Cant update data from server or Dont have information about ID");
					$this->response($error,500);	
				}
			}else
			{
				$error = array('status' => "Failed", 'Message' => "Cant find id from Client");
				$this->response($error,204);
			}
			
		}
	}
	
	private function updates()
	{
		
	}
	/*********************************/
	/*
	*
	*/ // DELETE
	/*********************************/
	private function delete()
	{
		$this->_check_request_method('DELETE');
		$id = $_REQUEST['id'];
		if(!empty($id))
		{
			$func_method = 'delete'.$this->class_name;
			$result = $this->APIclass->$func_method($id);
			if($result>0)
			{
				$success = array ('Status'=>'Success', 'Message'=>'Complete to delete '.$result.' an Object', 'Number'=>$result );
				$this->response($success,200);
			}
			else
			{
				$error = array ('Status'=>'Failed', 'Message'=>' Cant delete from Server');
				$this->response($error,500);
			}
		}else
		{
			$error = array('status' => "Failed", 'Message' => "No Content");
			$this->response($error,204);	// If no records "No Content" status
		}
	}
	
	private function deleteAll()
	{
		$this->_check_request_method('DELETE');
		if($this->APIclass->deleteAll())
		{
			$mess = array('status'=>'Success', 'Message'=>'Complete to delete all');
			$this->response($mess,200);
		}
		else
		{
			$error = array('status' => "Failed", 'Message' => "No Content");
			$this->response($error,204);	// If no records "No Content" status
		}	
	}
	private function _check_request_method($method)
	{
		if($this->_get_request_method()!= $method )
		{
			$error = array('status' => "Failed", 'Message' => "Not Acceptable");
			$this->response($error,406);
			return false;
		}
		return true;
	}

	/*private function requestImage()
	{
		$image1 = file_get_contents('C:\\xampp\\htdocs\\RESTphp\\resource\\Restaurant\\Res_LyClub.jpg');
		$im1 = base64_encode($image1);
		$image2 = file_get_contents('C:\\xampp\\htdocs\\RESTphp\\resource\\Restaurant\\Res_Sen_TayHo.jpg');
		$im2 = base64_encode($image2);
		$image3 = file_get_contents('C:\\xampp\\htdocs\\RESTphp\\resource\\Restaurant\\Res_LucThuy.jpg');
		$im3 = base64_encode($image3);
		$image4 = file_get_contents('C:\\xampp\\htdocs\\RESTphp\\resource\\Restaurant\\Res_donsHoTay.jpg');
		$im4 = base64_encode($image4);
		$result = array('Image1'=>$im1, 'Image2'=>$im2, 'Image3'=>$im3, 'Image4'=>$im4);
		$this->response($result,200);
	}*/
	
	public function getAuthor()
	{
		echo $_SERVER['PHP_AUTH_DIGEST'];
		echo $_SERVER['PHP_AUTH_USER'];
		echo $_SERVER['PHP_AUTH_PW'];
	}
}
$API = new AppInterface();
$API->processAPI();
/*$id1 = "APA91bGDr-lteHcGqhJjBGRuGCs7BwmuuY9c3Xw-fRgDLJ7c68jAIuiDW0vCKnuu9bzrQwXEJFyYe1nUWTNXhcwPN516_KCzasrX5wiRYdBVz1YFBXDmOgLsjDpcUdUElzF-iRR3ENgi8llKuW_e5RGukoP7RDJybYWotVvZKvG7ZXSAqz6zuB8";
$id2 = "APA91bFlKvveBtB2eld5Pd2bv8RQD8TGoFfVmg1eP-sePT2pR5kQuFhziV50yNWw9WTBwJfdq0fY2Pmxo4G7EMv-LsFM6YgDRuAjFwUJBPlgdZTlxOZOweF54FDAWEnFJs3sT-7HYTvD7w7SDNxqpxlPgv4YCFGXMw";
$resID = array($id1,$id2);
$mess = array('message'=>'Hehehehe');
$API->sendNotification($resID,$mess);*/
?>